package com.wan37.logic.shop.handler;

import com.wan37.logic.component.LogicWorkerHandlerCenter;
import com.wan37.logic.component.Worker;
import com.wan37.logic.domain.Result;
import com.wan37.logic.shop.domain.vo.CommodityConfigVO;
import com.wan37.logic.domain.vo.MessageVO;
import com.wan37.logic.global.reference.SpringContextHolder;
import com.wan37.logic.handler.MsgHandler;
import com.wan37.logic.shop.cache.CommodityServiceCache;
import com.wan37.logic.utils.RouteUtils;
import com.wan37.protobuf.constant.CommonShop;
import com.wan37.protobuf.message.client2server.game.GameShop;

import java.util.ArrayList;
import java.util.List;

/**
 * @author : luoyong
 * @date : 2020-07-06 11:25
 **/
public class CGameShopHandler extends MsgHandler {
    private RouteUtils routeUtils;
    private CommodityServiceCache commodityServiceCache;
    private LogicWorkerHandlerCenter logicWorkerHandlerCenter;

    /**
     * MsgHandler构造函数
     *
     * @param messageVO
     */
    public CGameShopHandler(MessageVO messageVO) {
        super(messageVO);
        this.routeUtils = SpringContextHolder.getBean("routeUtils");
        this.commodityServiceCache = SpringContextHolder.getBean("commodityServiceCache");
        this.logicWorkerHandlerCenter = SpringContextHolder.getBean("logicWorkerHandlerCenter");
    }

    @Override
    public void execute(Worker worker) throws Exception {
        GameShop.CGameShopMsg cGameShopMsg = (GameShop.CGameShopMsg) messageVO.getMessage();
        int code = cGameShopMsg.getCode();
        String userId = messageVO.getUserId();
        String commodityId = cGameShopMsg.getCommodityId();
        int count = cGameShopMsg.getCount();
        switch (code) {
            case CommonShop.GAME_GET_ALL_COMMODITY:
                List<CommodityConfigVO> commodityConfigVOList = new ArrayList<>();
                commodityServiceCache.getCommodityInMap().values()
                        .forEach(commodityIn -> commodityConfigVOList.add(commodityIn.getCommodityConfigVO()));
                routeUtils.sendShopResponse(code, "获取成功！", commodityConfigVOList, messageVO.getUserId());
                break;
            case CommonShop.GAME_SHOP_COMMODITY:
                //根据商品Id进行单线程处理，避免出现线程安全问题
                logicWorkerHandlerCenter.distributeWork(commodityId, new ShopHandler(userId, commodityId, count,"1001"));
                break;
            default:

                break;
        }
    }
}
